<template>
  <div>
    <el-dialog
      title="请选择客户"
      :visible.sync="customerDialog"
      height="450"
      width="70%"
      @open="onSearch()"
      @close="cancelDialog()"
    >
      <el-container>
        <el-header style="height: auto">
          <el-form ref="queryForm" :inline="true" :model="query" class="demo-form-inline">
            <el-form-item label="uid" prop="uid">
              <el-input v-model="query.userId" placeholder="请输入uid" />
            </el-form-item>
            <el-form-item label="用户名" prop="username">
              <el-input v-model="query.username" placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="query.phone" placeholder="请输入手机号" />
            </el-form-item>
            <el-form-item>
              <el-button @click="onSearch()">
                搜索
              </el-button>
            </el-form-item>
          </el-form>
        </el-header>
        <el-main>
          <el-table
            ref="customerTable"
            :data="data"
            height="300"
            style="width: 100%"
          >
            <el-table-column
              prop="userId"
              label="uid"
            />
            <el-table-column
              prop="username"
              label="用户名"
            />
            <el-table-column
              prop="phone"
              label="手机号"
            />
            <el-table-column
              fixed="right"
              label="操作"
              width="100"
            >
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="relateClick(scope.row)">
                  选择
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
        <el-footer align="middle">
          <el-pagination
            @size-change="sizeChange"
            @current-change="currentChange"
            :current-page="page.current"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.total">
          </el-pagination>
        </el-footer>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import {fetchList} from '@/api/user/customer'

export default {
  name: 'chose-customer',
  props: {
    customerDialog: Boolean,
    customerId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      query: {
        userId: null,
        username: '',
        phone: '',
      },
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      data: [],
      relateData: {},
    }
  },
  methods: {
    // 确认
    confirmDialog() {
      this.$emit('setValue', this.relateData)
      this.relateData = {}
      this.cancelDialog()
    },
    // 关闭
    cancelDialog() {
      this.$refs['queryForm'].resetFields()
      this.$emit('closeDialog', false)
    },
    onSearch() {
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.current = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page, this.query);
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      }, () => {
        this.loading = false;
      });
    },
    relateClick(val) {
      this.relateData = val;
      this.confirmDialog();
    }
  }
}
</script>

<style>

</style>
